/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package protocol;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import server.managerServer.executors.ExecutorConnect;
import server.managerServer.executors.ExecutorCreatePoll;

/**
 *
 * @author radu
 */
public class MessageCreatePollReq extends Message {

    String[] optionList;

    public MessageCreatePollReq(Element root) {

        NodeList options = root.getElementsByTagName("option");
        //TODO : more testing on correcteness of the message
        //TODO: ensure that the length of options is > 0
        optionList = new String[options.getLength()];
        for (int i = 0; i < options.getLength(); i++) {
            
            Node option = options.item(i);

            if (option.getNodeType() == Node.ELEMENT_NODE) {
                Element optionElem = (Element) option;

                //TODO : test that ord is not bigger than the number of options

                //TODO : ensure that the same ord is not given twice
                int ord = Integer.valueOf(optionElem.getAttribute("ord"));
                optionList[ord] = optionElem.getTextContent();
            }
        }
    }

    public MessageCreatePollReq(String[] options) {
        optionList = options;
    }

    @Override
    public String toString() {
        String str = "<create>";

        for (int i = 0; i < optionList.length; i++) {
            str += "<option ord=\"" + i + "\">" + optionList[i] + "</option>";
        }
        str += "</create>\r\n";

        return str;
    }

    @Override
    public Executor getServerExecutor() {
        return new ExecutorCreatePoll(this);
    }

    public String[] getOptionList() {
        return optionList;
    }
}
